package com.dai.wechat.util;

import com.alibaba.fastjson.JSONObject;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

import static com.dai.wechat.util.Memorial.getLeapYear;

/**
 *@ClassName Pusher
 *@Description TODO
 *@Author ydzhao
 *@Date 2022/8/2 
 */
public class Pusher {
    public static String appId = "wx20429c456f923928";
    public static String secret = "98c9c5ef8617888242fa8ba7a6a1a466";
    //模版id
    public static String DRINK_TEMPLATEID = "YWSkyTMH1Y_cuKjcw8OyONfzBxUKzxE-3wI-ZGkF63A";
    //public static String GOODMORNING_TEMPLATEID = "C69En65f6aCkUcVw5elsVzTjFkEJtiy2tk-eLUhxYuo";
    public static String GOODMORNING_TEMPLATEID = "fW5dVVh4DpfZ5-qW5_PY2xHKKVUh0xoN5LwYLej0rbI";
    public static String CONSTELLATION_TEMPLATEID = "evhhMKsrczceL1vE7m01nOt4esyEbYS-HYiZF0BXmuY";



    public static Result<String> push(String openId,String templateId){
        //1，配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(appId);
        wxStorage.setSecret(secret);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);

        //2 设置模板消息
        WxMpTemplateMessage templateMessage = getTemplateMessage(openId,templateId);

        try {
            // 推送消息
            String s = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            return Result.suceessWithoutData(s);

        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
            return Result.failed("500","推送失败：" + e.getMessage());
        }
    }

    private static WxMpTemplateMessage getTemplateMessage(String openId, String templateId) {

        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openId)
                .templateId(templateId)
                .build();

        if (GOODMORNING_TEMPLATEID.equals(templateId)) goodMorning(templateMessage);
        else if (DRINK_TEMPLATEID.equals(templateId)) drinkWater(templateMessage);
        else if (CONSTELLATION_TEMPLATEID.equals(templateId)) forecast(templateMessage);


        return templateMessage;

    }

    public static void goodMorning(WxMpTemplateMessage templateMessage){

        // 配置信息
        List<String> weather1 = ApiUtil.getWeather("南昌");
        String week = ApiUtil.dateToWeek(weather1.get(0));
        templateMessage.addData(new WxMpTemplateData("data",weather1.get(0) + "  "+ week ,"#00BFFF"));
        templateMessage.addData(new WxMpTemplateData("weather", weather1.get(3) ,"#173177"));
        templateMessage.addData(new WxMpTemplateData("lowTemperature", weather1.get(1) + " °C","#FF6347" ));
        templateMessage.addData(new WxMpTemplateData("highTemperature",weather1.get(2) + " °C","#FF69B4"));
        templateMessage.addData(new WxMpTemplateData("memorial",Memorial.getLoveDay() +"","#FF1493"));
        templateMessage.addData(new WxMpTemplateData("meet",Memorial.getBirthDay() +"","#FFA500"));
        templateMessage.addData(new WxMpTemplateData("saying",  ApiUtil.getRainbow() + "\n","#C71585"));
        templateMessage.addData(new WxMpTemplateData("english", ApiUtil.getEnglish() + "","#FF6347"));
        String beizhu = "今天也是好爱好爱你的一天~";
        if(Memorial.getLoveDay() % getLeapYear() == 0){
            int count = (Memorial.getLoveDay() + 1) % getLeapYear();
            beizhu = "今天是戴涛和滕敏的" + count + "周年纪念日！";
        }

        if(Memorial.getBirthDay() % getLeapYear() == 0){

            beizhu = "生日快乐！滕敏小公主~";
        }

        templateMessage.addData(new WxMpTemplateData("note",beizhu,"#FF0000"));
    }


    public static void drinkWater(WxMpTemplateMessage templateMessage){

        // 配置信息
        List<String> rhesis = ApiUtil.getRhesis();
        String message = "按时喝水的宝宝最乖了。";
        templateMessage.addData(new WxMpTemplateData("message", message,"#C71585"));
        templateMessage.addData(new WxMpTemplateData("context", rhesis.get(0),"#173177"));
        templateMessage.addData(new WxMpTemplateData("author", rhesis.get(1),"#173177"));

    }


    public static void forecast(WxMpTemplateMessage templateMessage){

        HashMap<String, String> constellation = ApiUtil.getConstellation();
        // 不处理异常
        String synthetical = constellation.get("综合指数");
        String love = constellation.get("爱情指数");
        String work = constellation.get("工作指数");
        String fortune = constellation.get("财运指数");
        String healthy = constellation.get("健康指数");
        String luckColor = constellation.get("幸运颜色");
        String luckNum = constellation.get("幸运数字");
        String noble = constellation.get("贵人星座");
        String conclusion = constellation.get("今日概述");
        templateMessage.addData(new WxMpTemplateData("synthetical",synthetical,"#00BFFF"));
        templateMessage.addData(new WxMpTemplateData("love",love,"#00FFFF"));
        templateMessage.addData(new WxMpTemplateData("work",work,"#173177"));
        templateMessage.addData(new WxMpTemplateData("fortune",fortune,"#FF6347" ));
        templateMessage.addData(new WxMpTemplateData("healthy",healthy,"#FF69B4"));
        templateMessage.addData(new WxMpTemplateData("luckColor",luckColor,"#FF1493"));
        templateMessage.addData(new WxMpTemplateData("luckNum",luckNum,"#FFA500"));
        templateMessage.addData(new WxMpTemplateData("noble",noble,"#C71585"));
        templateMessage.addData(new WxMpTemplateData("conclusion",conclusion,"#FF6347"));

    }



}

